import {HintUI} from "./HintUI";

const {ccclass, property} = cc._decorator;
@ccclass
export default class LoadingUI extends cc.Component {
    @property(cc.Node)
    animNode:cc.Node = null;
    @property(cc.Label)
    loadingLabel:cc.Label = null;
    handler_timeout;
    static TIME_OUT:number = 5;

    static show(text:string,timeoutText?:string,timeout?:number){
        let node = cc.find("Canvas/加载动画");
        if(!node) return;
        let component = node.getComponent(LoadingUI)
        if(!component) return;
        component._show(text,timeoutText,timeout);
    }

    static exit(){
        let node = cc.find("Canvas/加载动画");
        if(!node) return
        let component = node.getComponent(LoadingUI)
        if(!component) return
        component._exit();
    }

    private _show(text:string,timeoutText?:string,timeout?:number){
        if(timeout == null) timeout = LoadingUI.TIME_OUT;
        this._exit();
        this.node.active = true;
        this.animNode.runAction(cc.repeatForever(cc.rotateBy(1,360)));
        this.loadingLabel.string = text;
        this.handler_timeout = setTimeout(function () {
            if(timeoutText && timeoutText != ""){
                HintUI.show(timeoutText);
            }
            this._exit();
        }.bind(this),timeout*1000);
    }

    private _exit(){
        this.node.active = false;
        this.animNode.stopAllActions();
        if(this.handler_timeout){
            clearTimeout(this.handler_timeout);
            this.handler_timeout = null;
        }
    }
}